package com.cxs;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @Project: wanju-weixin
 * @Author: cxs2014501@163.com
 * @Create: 2023/1/13 15:58
 * @Description:
 **/
public class Wanju {

    private static final String FIREND_NAME = "胡子";

    public static void main(String[] args) {
        Robot robot = getRobot();
        if (robot == null) {
            return;
        }
        // 打开微信
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_W);
        // 释放按键
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_ALT);

        // 延迟一下
        robot.delay(1000);

        // 搜索用户
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_F);

        robot.keyRelease(KeyEvent.VK_CONTROL);

        // 输入好友昵称
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable friendNime = new StringSelection(getFriendNime());
        clipboard.setContents(friendNime, null);

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.delay(1000);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(1000);

        sentMsgToFriend();

    }

    /**
     * 发送消息给好友
     */
    public static void sentMsgToFriend() {
        // 读取消息文件
        InputStream in = null;
        InputStreamReader reader = null;
        BufferedReader br = null;
        try {
            in = ClassLoader.getSystemClassLoader().getResourceAsStream("message.txt");
            reader = new InputStreamReader(in, "utf-8");
            br = new BufferedReader(reader);
            String msg = null;
            while ((msg = br.readLine()) != null) {
                sendMsgToFriendOne(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (reader != null) {
                    reader.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 发送单条
     * @param msg
     */
    private static void sendMsgToFriendOne(String msg) {
        Robot robot = getRobot();
        if (robot == null) {
            return;
        }
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable text = new StringSelection(msg);
        clipboard.setContents(text, null);
        
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        
        robot.keyRelease(KeyEvent.VK_CONTROL);
        
        robot.delay(1000);
        
        robot.keyPress(KeyEvent.VK_ENTER);

        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.delay(1000);
    }

    /**
     * 获取robot对象
     * @return
     */
    private static Robot getRobot(){
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return robot;
    }

    /**
     * 输入好友昵称
     * @return
     */
    public static String getFriendNime() {
        return FIREND_NAME;
    }
}
